package com.xoa.service.party;

import com.xoa.dao.party.GeneralSafetyUnitMapper;
import com.xoa.model.party.GeneralSafetyUnitWithBLOBs;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GeneralSafetyUnitService {
    @Autowired
    private GeneralSafetyUnitMapper generalSafetyUnitMapper;

    /**
     * 可选择字段增加
     * @param generalSafetyUnitWithBLOBs
     * @return
     */
    @Transactional
    public ToJson insertSelective(GeneralSafetyUnitWithBLOBs generalSafetyUnitWithBLOBs){
        ToJson toJson=new ToJson(1,"error");
        int temp=generalSafetyUnitMapper.insertSelective(generalSafetyUnitWithBLOBs);
        if(temp>0){
            toJson.setFlag(0);
            toJson.setMsg("插入成功");
        }
        return toJson;
    }

    /**
     * 根据id删除
     * @param generalSafetyUnitId
     * @return
     */
    @Transactional
    public ToJson deleteBygeneralSafetyUnitId(int generalSafetyUnitId){
        ToJson toJson=new ToJson(1,"error");
        int temp=generalSafetyUnitMapper.deleteByPrimaryKey(generalSafetyUnitId);
        if(temp>0){
            toJson.setFlag(0);
            toJson.setMsg("删除成功");
        }
        return toJson;

    }

    /**
     * 修改
     * @param generalSafetyUnitWithBLOBs
     * @return
     */
    @Transactional
    public ToJson updateByPrimaryKeySelective(GeneralSafetyUnitWithBLOBs generalSafetyUnitWithBLOBs){
        ToJson toJson=new ToJson(1,"error");
        int temp=generalSafetyUnitMapper.updateByPrimaryKeySelective(generalSafetyUnitWithBLOBs);
        if(temp>0){
            toJson.setFlag(0);
            toJson.setMsg("修改成功");
        }
        return toJson;
    }

    /**
     * 根据id查询
     * @param generalSafetyUnitId
     * @return
     */
    @Transactional
    public ToJson selectBygeneralSafetyUnitId(int generalSafetyUnitId){
        ToJson<GeneralSafetyUnitWithBLOBs> toJson=new ToJson<GeneralSafetyUnitWithBLOBs>(1,"error");
        GeneralSafetyUnitWithBLOBs temp=generalSafetyUnitMapper.selectByPrimaryKey(generalSafetyUnitId);
        if(temp!=null){
            toJson.setObject(temp);
            toJson.setFlag(0);
            toJson.setMsg("查询成功");
        }
        return toJson;
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param useFlag
     * @param generalSafetyUnitWithBLOBs
     * @return
     */
    @Transactional
    public ToJson selectSelective(Integer page, Integer pageSize, boolean useFlag,
                                  GeneralSafetyUnitWithBLOBs generalSafetyUnitWithBLOBs){
        ToJson<GeneralSafetyUnitWithBLOBs> toJson=new ToJson<GeneralSafetyUnitWithBLOBs>(1,"error");
        try {
            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            pageParams.setUseFlag(useFlag);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("page", pageParams);
            map.put("record",generalSafetyUnitWithBLOBs);
            List<GeneralSafetyUnitWithBLOBs> templist=generalSafetyUnitMapper.selectSelective(map);
            toJson.setTotleNum(pageParams.getTotal());
            toJson.setObj(templist);
            toJson.setFlag(0);
            toJson.setMsg("success");
        }
        catch (Exception e){
            toJson.setMsg(e.getMessage());
            L.e("PzHouseServiceController selServiceByCond:" + e);
        }
        return toJson;
    }

}
